package com.zimi.service;

import com.zimi.mapper.WfSystaskKeyMapper;
import com.zimi.mapper.WfWorkflowMapper;
import com.zimi.mapper.WfWorkflowVOMapper;
import com.zimi.model.WfSystaskKey;
import com.zimi.model.WfWorkflow;
import com.zimi.model.WfWorkflowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WfWorkflowService {

    @Autowired
    WfWorkflowMapper wfWorkflowMapper;
    @Autowired
    WfWorkflowVOMapper wfWorkflowVOMapper;
    @Autowired
    WfSystaskKeyMapper wfSystaskKeyMapper;


    public int deleteByPrimaryKey(Integer id) {
        return wfWorkflowMapper.deleteByPrimaryKey(id);
    }


    public int insert(WfWorkflow record) {
        return wfWorkflowMapper.insert(record);
    }


    public int insertSelective(WfWorkflow record) {
        return wfWorkflowMapper.insertSelective(record);
    }


    public WfWorkflow selectByPrimaryKey(Integer id) {
        return wfWorkflowMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(WfWorkflow record) {
        return wfWorkflowMapper.updateByPrimaryKeySelective(record);
    }


    public int updateByPrimaryKey(WfWorkflow record) {
        return wfWorkflowMapper.updateByPrimaryKey(record);
    }


    public List<WfWorkflow> selectAll(WfWorkflow record) {

        return wfWorkflowMapper.selectAll(record);
    }

    //根据表单id查询
    public List<WfWorkflow> selectWfWorkflowByFormId(int fid) {
        return wfWorkflowMapper.selectWfWorkflowByFormId(fid);
    }

    /*
     * @author: wj
     * @description: 新建流程
     * @date: 2019/3/29 17:54
     * @param
     * @return
     */
    public int saveNewWorkFlow(WfWorkflow wfWorkflow) {
        WfWorkflow tempWfWorkflow = new WfWorkflow();
        if (wfWorkflow.getFormEditId() != null && wfWorkflow.getFormEditId() != 0) {
            tempWfWorkflow.setFormEditId(wfWorkflow.getFormEditId());
            List<WfWorkflow> wfWorkflowList = wfWorkflowMapper.selectAll(tempWfWorkflow);
            if (wfWorkflowList != null && wfWorkflowList.size() >= 1) {
                if (wfWorkflow.getWfId() != null && wfWorkflow.getWfId() != 0) {
                    for (WfWorkflow eveWfWorkflow : wfWorkflowList) {
                        if (!wfWorkflow.getFormEditId().equals(eveWfWorkflow.getWfId())) {
                            return 0;// "此表单已经绑定到别的流程，绑定失败！";
                        }
                    }
                } else {
                    return 0;//"此表单已经绑定到别的流程，绑定失败！";
                }
            }
        }
        if (wfWorkflow.getActiveFlag() == null) {
            wfWorkflow.setActiveFlag(0);
        }
        wfWorkflow.setDeleteFlag(1);
        wfWorkflowMapper.insertSelective(wfWorkflow);
        int wfId = wfWorkflowMapper.lastInsertId();
        return wfId;
    }

    /*
     * @author: wj
     * @description: 流程上移
     * @date: 2019/4/8 10:05
     * @param
     * @return
     */
    public String moveUp(Integer wfId) {
        WfWorkflowVO tempWfWorkflowVO = new WfWorkflowVO();
        tempWfWorkflowVO.setCurWfId(wfId);
        List<WfWorkflowVO> list = wfWorkflowVOMapper.selectAllVO(tempWfWorkflowVO);
        int index = -1;
        if (list != null && list.size() > 0) {
            for (int i = 0, j = list.size(); i < j; i++) {
                if (list.get(i).getWfId().equals(wfId)) {
                    index = i;
                    tempWfWorkflowVO = list.get(i);
                }
            }
        }
        if (index == 0) {
            return "已到达顶部";
        } else {
            if (tempWfWorkflowVO.getWorkflowSort() != null) {
                tempWfWorkflowVO.setWorkflowSort(tempWfWorkflowVO.getWorkflowSort().intValue() - 1);
                wfWorkflowVOMapper.updateWorkFlow(tempWfWorkflowVO);
            }

            WfWorkflowVO wfWorkflowVO = list.get(index - 1);
            if (wfWorkflowVO.getWorkflowSort() != null) {
                wfWorkflowVO.setWorkflowSort(wfWorkflowVO.getWorkflowSort().intValue() + 1);
                wfWorkflowVOMapper.updateWorkFlow(wfWorkflowVO);
            }
        }
        return "上移成功";
    }

    /*
     * @author: wj
     * @description: 流程下移
     * @date: 2019/4/8 10:05
     * @param
     * @return
     */
    public String moveDown(Integer wfId) {
        WfWorkflowVO tempWfWorkflowVO = new WfWorkflowVO();
        tempWfWorkflowVO.setCurWfId(wfId);
        List<WfWorkflowVO> list = wfWorkflowVOMapper.selectAllVO(tempWfWorkflowVO);
        int index = -1;
        if (list != null && list.size() > 0) {
            for (int i = 0, j = list.size(); i < j; i++) {
                if (list.get(i).getWfId().equals(wfId)) {
                    index = i;
                    tempWfWorkflowVO = list.get(i);
                }
            }
        }
        if (index == (list.size() - 1)) {
            return "已到达底部";
        } else {
            tempWfWorkflowVO.setWorkflowSort(tempWfWorkflowVO.getWorkflowSort().intValue() + 1);
            wfWorkflowVOMapper.updateWorkFlow(tempWfWorkflowVO);

            WfWorkflowVO wfWorkflowVO = list.get(index + 1);
            wfWorkflowVO.setWorkflowSort(wfWorkflowVO.getWorkflowSort() - 1);
            wfWorkflowVOMapper.updateWorkFlow(wfWorkflowVO);
        }
        return "下移成功";
    }

    /*
     * @author: wj
     * @description: 查询流程状态
     * @date: 2019/4/9 14:52
     * @param
     * @return
     */
    public boolean getWorkFlowState(Integer wfId) {
        boolean flag = true;
        WfWorkflow wfWorkflow = wfWorkflowMapper.selectByPrimaryKey(wfId);
        if (wfWorkflow == null) throw new RuntimeException("流程已不存在");
        if (wfWorkflow.getActiveFlag().intValue() != 3) {
            WfSystaskKey wfSystaskKey = new WfSystaskKey();
            wfSystaskKey.setWfId(wfId);
            List<WfSystaskKey> wfSystaskKeyList = wfSystaskKeyMapper.selectAll(wfSystaskKey);
            if (wfSystaskKeyList != null && wfSystaskKeyList.size() > 0) {
                for (WfSystaskKey eveWfSystaskKey : wfSystaskKeyList) {
                    if (eveWfSystaskKey.getSysTaskKeyState().intValue() != 1) {
                        flag = false;
                        break;
                    }
                }
            }
        }
        return flag;
    }
}
